import 'package:flutter/material.dart';
import 'package:flutter_app_unionad/core/configuration.dart';
import 'package:flutter_app_unionad/model/exchange_list_model.dart';
import 'package:flutter_app_unionad/request/facility_request.dart';
import 'package:flutter_app_unionad/ui/user_center/widget/recharge_item_widget.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:core_package/ui/widget/my_asset_image.dart';
import 'package:flutter/material.dart';
import 'package:core_package/core_package.dart';
import 'package:flutter_app_unionad/model/recharge_order_model.dart';

//资金明细 --兑换
class FinancialDetailTwoWidget extends StatefulWidget {
  @override
  _FinancialDetailTwoWidgetState createState() => _FinancialDetailTwoWidgetState();
}

class _FinancialDetailTwoWidgetState extends State<FinancialDetailTwoWidget> {
  int page = 0;
  EasyRefreshController? _controller;
  List<ExchangeListModel>? dataSource;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = EasyRefreshController();
    getData();
  }

  void getData() {
    page = 1;
    FacilityRequest.exchangeDetailList(pageNum: page).then((value) {
      if (value != null) {
        if (dataSource == null) dataSource = [];
        dataSource?.clear();
        dataSource?.addAll(value);
      }
      if (mounted) setState(() {});
    });
  }

  void getMoreData() {
    page++;
    FacilityRequest.exchangeDetailList(pageNum: page).then((value) {
      if (value != null) {
        if (dataSource == null) dataSource = [];
        dataSource?.addAll(value);
      }
      if (dataSource != null && dataSource!.length < 20) {
        _controller!.finishLoadCallBack?.call();
      } else {
        _controller!.resetLoadStateCallBack?.call();
      }
      if (mounted) setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    if (dataSource == null) {
      return ProgressDialog.showFirstLoading();
    }
    if (dataSource!.length == 0) {
      return Center(child: EmptyWidget());
    }
    return EasyRefresh(
      header: RefreshBasics.header,
      footer: RefreshBasics.footer,
      controller: _controller,
      onRefresh: () async {
        getData();
      },
      onLoad: () async {
        getMoreData();
      },
      child: ListView.builder(
        padding: EdgeInsets.zero,
        itemCount: dataSource!.length,
        itemBuilder: (ctx, index) {
          return ExchangeItemWidget(
            model: dataSource![index],
          );
        },
      ),
    );
  }
}

class ExchangeItemWidget extends StatelessWidget {
  final bool isRecharge;
  final ExchangeListModel? model;

  const ExchangeItemWidget({Key? key, this.isRecharge = true, this.model}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // String day;
    // bool userType = (model.userType == '03' || model.userType == '04');
    // if (userType) {
    //   day = isRecharge ? "充值流量" : '兑现流量';
    // } else {
    //   day = isRecharge ? "充值天数" : '兑现天数';
    // }
    return Container(
        margin: EdgeInsets.only(bottom: 10.px),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(5.px),
        ),
        padding: EdgeInsets.only(
          left: 37.px,
          right: 20.px,
          top: 20.px,
          bottom: 20.px,
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            builtItem(
              image: "icon_order_1",
              imageSize: Size(8, 12),
              title: "设备编号",
              detail: UserCacheUil.getString(Configuration.deviceId).toString(),
            ),
            MySizeBox(height: 5),
            builtItem(
              image: "icon_order_2",
              imageSize: Size(12, 12),
              title: '兑换天数',
              detail: '${model!.amount} 天',
            ),
            MySizeBox(height: 5),
            builtItem(
              image: "icon_order_3",
              imageSize: Size(10, 13),
              title: "兑现金币",
              detail: '${model!.points.toString()}元',
            ),
            MySizeBox(height: 5),
            builtItem(
              image: "icon_order_4",
              imageSize: Size(12, 12),
              title: "兑换时间",
              detail: DateTime.fromMillisecondsSinceEpoch(model!.applyTime!).toString().split('.').first,
            ),
            MySizeBox(height: 5),

            builtItem(
              image: "icon_order_5",
              imageSize: Size(12, 12),
              title: "类型",
              detail: "兑换天数",
              detailColor: MyColors.formHex("#4FD8FFFF"),
            ),
            if (model!.serviceDate != null) MySizeBox(height: 5),

            // if (model!.serviceDate !=null && model!.serviceDate != '')
            if (model!.serviceDate != null)
              builtItem(
                image: "icon_order_2",
                imageSize: Size(12, 12),
                title: '服务日期',
                detail: model!.serviceDate ?? '',
              ),
          ],
        ));
  }

  Widget builtItem({
    String? image,
    String? title,
    Size? imageSize,
    String? detail,
    Color? detailColor,
  }) {
    return Row(
      children: [
        Container(
          width: 13.px,
          alignment: Alignment.center,
          child: MyAssetImage(
            image ?? '',
            width: imageSize!.width.px,
            height: imageSize.height.px,
            fit: BoxFit.fill,
          ),
        ),
        MySizeBox(width: 15),
        Text(
          "$title：",
          style: TextStyle(
            color: MyColors.formHex("#1D242CFF"),
            fontSize: MyFont.font13,
            fontWeight: MyFontWeight.regular,
          ),
        ),
        Text(
          detail ?? '',
          style: TextStyle(
            decoration: detailColor != null ? TextDecoration.underline : TextDecoration.none,
            color: detailColor ?? MyColors.formHex("#1D242CFF"),
            fontSize: MyFont.font13,
            fontWeight: MyFontWeight.regular,
          ),
        )
      ],
    );
  }
}
